package com.hamizhong.iflowers.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hamizhong.iflowers.db.dao.hibernate.ICommentDao;
import com.hamizhong.iflowers.db.dao.hibernate.IGoodsDao;
import com.hamizhong.iflowers.db.dao.hibernate.IOrdersDao;
import com.hamizhong.iflowers.db.dao.hibernate.IUserDao;
import com.hamizhong.iflowers.db.entity.Comment;
import com.hamizhong.iflowers.db.entity.Goods;
import com.hamizhong.iflowers.db.entity.Orders;
import com.hamizhong.iflowers.db.entity.User;
import com.hamizhong.iflowers.service.ICommentService;
import com.hamizhong.iflowers.service.base.MyBaseService;
import com.hamizhong.iflowers.utils.MyTools;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.CommentVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;

public class CommentServiceImpl extends MyBaseService implements ICommentService {
	
private ICommentDao iCommentDao = null;
private IGoodsDao iGoodsDao = null;
private IOrdersDao iOrdersDao = null;
private IUserDao iUserDao = null;

public ICommentDao getiCommentDao() {
	return iCommentDao;
}
public void setiCommentDao(ICommentDao iCommentDao) {
	this.iCommentDao = iCommentDao;
}
public IGoodsDao getiGoodsDao() {
	return iGoodsDao;
}
public void setiGoodsDao(IGoodsDao iGoodsDao) {
	this.iGoodsDao = iGoodsDao;
}
public IOrdersDao getiOrdersDao() {
	return iOrdersDao;
}
public void setiOrdersDao(IOrdersDao iOrdersDao) {
	this.iOrdersDao = iOrdersDao;
}
public IUserDao getiUserDao() {
	return iUserDao;
}
public void setiUserDao(IUserDao iUserDao) {
	this.iUserDao = iUserDao;
}

public IOPResultDto<IObjectVO> findByGoodsId(Integer goodsId, Integer pageNum, int pageSize) {
	IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
	List<Comment> commentList = iCommentDao.findByGoodsId(goodsId, pageNum, pageSize);
	List<CommentVO> commentVOList = new ArrayList<CommentVO>();
	CommentVO commentVO = null;
	Orders order = null;
	User user = null;
	if(commentList != null) {
		for (Comment comment : commentList) {
			commentVO = new CommentVO();
			BeanUtils.copyProperties(comment, commentVO);
			commentVO.setCreateDateStr(MyTools.formatDttm2YYYY_MM_DD(commentVO.getCreateDate()));
			
			order = iOrdersDao.findById(comment.getOrders().getOrderId());
			user = iUserDao.findById(order.getUser().getUserId());
			commentVO.setUserName(user.getUserName());
			
			commentVOList.add(commentVO);
		}
	}
	IObjectVO iObjectVO = new IObjectVO();
	iObjectVO.setCommentVOList(commentVOList);
	dto.addToResultList(iObjectVO);
	dto.setSuccess(true);
	return dto;
}

}
